VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "sgWindowTemplate"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'--------------------------------------------------------------------------
' File: CSGWINDOWTEMPLATE.CLS
'
' This file is part of the 'Stinga sgWindow Wizard' project.
' Copyright (C) 1998 Stinga
' All rights reserved.
'--------------------------------------------------------------------------

Private mvarProject As String           ' Updated project
Private mvarCodeModule As String        ' File name of the module that will contain wizard code
Private mvarFirstMessage As Long        ' First message in the range of the handled messages
Private mvarLastMessage As Long         ' Last message in the range of the handled messages
Private mvarForm As String              ' Name of the subclassed form or form that contain subclassed control
Private mvarControl As String           ' Name of the sublassed control or empty string if no control is subclassed
Private mvarHandlerName As String       ' Handler variable name
Private mvarTemplateFile As String      ' Name of the file that contain code template
Private mvarTemplate As String          ' Code template

' sgWindow type library
Public TypeLib As TypeLibInfo

Const sparCodeModule = "CodeModule"
Const sparHandlerName = "HandlerName"
Const sparWindowHandle = "WindowHandle"
Const sparFirstMessage = "FirstMessage"
Const sparLastMessage = "LastMessage"



Private Function MsgName(msg&)
    MsgName = "UNKNOWN"
    Dim mi As MemberInfo
    For Each mi In TypeLib.TypeInfos.NamedItem("WinMsg").Members
        If (mi.Value = msg) Then MsgName = mi.name: Exit Function
    Next
End Function

Public Sub Clear()
    mvarFirstMessage = wm_NULL
    mvarLastMessage = wm_NULL
    mvarHandlerName = ""
    mvarProject = ""
    mvarCodeModule = ""
    mvarForm = ""
    mvarControl = ""
    mvarTemplateFile = ""
    mvarTemplate = ""

    ' Load sgWindow type library
    Set TypeLib = New TypeLibInfo
    TypeLib.LoadRegTypeLib "{485B1F95-F7E3-11D1-9825-204C4F4F5020}", 1, 0, 0
End Sub

Public Function UpdateCodeModule(code As CodeModule, sName$) As Boolean
    On Error GoTo Error_

    UpdateCodeModule = False
    If (code Is Nothing) Then Exit Function
    
    ' Make sure that we have code template
    If (TemplateFile = "") Then Exit Function
    
    ' Create code template parser object
    Dim codeTemplate As sgCodeTemplate
    Set codeTemplate = New sgCodeTemplate
    codeTemplate.TemplateFile = TemplateFile
    codeTemplate.Parameters.Add sparHandlerName, HandlerName
    codeTemplate.Parameters.Add sparCodeModule, sName
    codeTemplate.Parameters.Add sparFirstMessage, MsgName(FirstMessage)
    codeTemplate.Parameters.Add sparLastMessage, MsgName(LastMessage)
    If (ControlName = "") Then
        codeTemplate.Parameters.Add sparWindowHandle, "Me.HWND"
    Else
        codeTemplate.Parameters.Add sparWindowHandle, ControlName & ".HWND"
    End If
    
    ' Parse declaration block
    Dim sCode$
    Dim block As CodeBlock
    codeTemplate.name = "Declare"
    If (codeTemplate.Blocks.Count < 1) Then RaiseError 1, " (Declare)"
    For Each block In codeTemplate.Blocks
        ' Create handler variable declaration if it does not exist
        On Error Resume Next
        Dim hndlr As Member
        Set hndlr = code.Members.Item(HandlerName)
        On Error GoTo Error_
        If (hndlr Is Nothing) Then
            block.InsertCode code
        End If
    Next
    
    ' Parse handler block
    codeTemplate.name = "Handle"
    If (codeTemplate.Blocks.Count < 1) Then RaiseError 1, " (Handle)"
    For Each block In codeTemplate.Blocks
        block.InsertCode code
    Next
    
    ' Parse init block
    codeTemplate.name = "Init"
    If (codeTemplate.Blocks.Count < 1) Then RaiseError 1, " (Init)"
    For Each block In codeTemplate.Blocks
        block.InsertCode code
    Next
    
    UpdateCodeModule = True
    Exit Function
Error_:
    MsgBox Error$
End Function

Public Property Let HandlerName(ByVal vData As String)
    mvarHandlerName = vData
End Property

Public Property Get HandlerName() As String
    HandlerName = mvarHandlerName
End Property

Public Property Get DescriptionText() As String
    
    ' Subclassed window description
    If FormName = "" And ControlName = "" Then
        DescriptionText = "There is no selected window": Exit Property
    End If
    If FormName <> "" And ControlName = "" Then
        DescriptionText = "Subclassed form is '" & FormName & "'" + vbCrLf
    End If
    If FormName <> "" And ControlName <> "" Then
        DescriptionText = "Subclassed control is '" & ControlName & _
                          "' located on the '" & FormName & "'" + vbCrLf
    End If
    
    ' Handled messages description
    If (FirstMessage <> wm_NULL) Then
        If (LastMessage = FirstMessage) Then
            DescriptionText = DescriptionText & _
                "Handled message is " & UCase(MsgName(FirstMessage)) & vbCrLf
        Else
            DescriptionText = DescriptionText & _
                "Handled message range is from " & _
                UCase(MsgName(FirstMessage)) & " to " & _
                UCase(MsgName(LastMessage)) & vbCrLf
        End If
    End If
    
    ' Handler object description
    If (HandlerName <> "") Then
        DescriptionText = DescriptionText & _
            "Handler object name is '" & HandlerName & "'"
    End If
End Property

Public Property Let ControlName(ByVal vData As String)
Attribute ControlName.VB_Description = "Subclassed control or Nothing if subclassed item is not control"
    mvarControl = vData
End Property

Public Property Get ControlName() As String
    ControlName = mvarControl
End Property

Public Property Let FormName(ByVal vData As String)
Attribute FormName.VB_Description = "Subclassed form or form that contain subclassed control."
    mvarForm = vData
End Property

Public Property Get FormName() As String
    FormName = mvarForm
End Property

Public Property Let TemplateFile(ByVal vData As String)
    mvarTemplateFile = vData
End Property

Public Property Get TemplateFile() As String
    TemplateFile = mvarTemplateFile
End Property

Public Property Let LastMessage(ByVal vData As Long)
    mvarLastMessage = vData
End Property

Public Property Get LastMessage() As Long
    LastMessage = mvarLastMessage
End Property

Public Property Let FirstMessage(ByVal vData As Long)
    mvarFirstMessage = vData
End Property

Public Property Get FirstMessage() As Long
    FirstMessage = mvarFirstMessage
End Property

Public Property Let CodeModule(ByVal vData As String)
    mvarCodeModule = vData
End Property

Public Property Get CodeModule() As String
    CodeModule = mvarCodeModule
End Property

Public Property Let Project(ByVal vData As String)
    mvarProject = vData
End Property

Public Property Get Project() As String
    Project = mvarProject
End Property

Private Sub Class_Initialize()
    mvarFirstMessage = wm_NULL
    mvarLastMessage = wm_NULL
    mvarHandlerName = ""
    
    ' Load sgWindow type library
    Set TypeLib = New TypeLibInfo
    TypeLib.LoadRegTypeLib "{485B1F95-F7E3-11D1-9825-204C4F4F5020}", 1, 0, 0
End Sub

Private Sub RaiseError(nErr%, Optional sText$ = "")
    Err.Raise vbError + 1, "sgWindowWizard", ErrMsg(nErr) + sText, 0, 0
End Sub

Private Function ErrMsg(nErr%)
    Select Case nErr
    Case 1
        ErrMsg = "Code block does not exist"
    
    Case Else
        ErrMsg = "Unknown error"
    End Select
End Function
